package com.usooft.metaTalent.core.base.sms.tencent;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import com.tencentcloudapi.sms.v20210111.models.SendStatus;
import com.usooft.framework.core.sms.SmsProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author Xandy
 * @version 1.0.0
 * @description
 * @date 2023/11/28
 */
@Component
@Slf4j
public class TencentSmsProvider implements SmsProvider {

    private final TencentConfig config;

    private final ObjectMapper objectMapper;

    public TencentSmsProvider(TencentConfig config, ObjectMapper objectMapper) {
        this.config = config;
        this.objectMapper = objectMapper;
    }

    @Override
    public void send(String template, Map<String, String> content, String... phoneNumbers) {
        String[] var4 = phoneNumbers;
        int var5 = phoneNumbers.length;

        for (int var6 = 0; var6 < var5; ++var6) {
            String phoneNumber = var4[var6];
            this.send(template, content, phoneNumber);
        }
    }

    private void send(String template, Map<String, String> content, String phoneNumber) {
        String templateParam;
        try {
            templateParam = this.objectMapper.writeValueAsString(content);
        } catch (Exception var9) {
            throw new RuntimeException(var9);
        }
        Credential cred = new Credential(config.getAccessKeyId(), config.getAccessKeySecret());
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint(config.getEndPoint());
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        SmsClient client = new SmsClient(cred, config.getRegionId(), clientProfile);

        // 实例化一个请求对象,每个接口都会对应一个request对象
        SendSmsRequest req = new SendSmsRequest();
        String[] phoneNumberSet = {phoneNumber};
        req.setPhoneNumberSet(phoneNumberSet);
        req.setSmsSdkAppId(config.getSmsSdkAppId());
        req.setSignName(config.getSignName());
        req.setTemplateId(config.getTemplateId());

        String[] templateParamSet = {templateParam};
        req.setTemplateParamSet(templateParamSet);
        SendSmsResponse resp = null;
        try {
            resp = client.SendSms(req);
        } catch (TencentCloudSDKException e) {
            log.error("短信发送失败，错误消息：" + e);
            throw new RuntimeException("短信发送失败，腾讯短信发送未返回响应");
        }
        SendStatus[] sendStatusSet = resp.getSendStatusSet();
        for (SendStatus sendStatus : sendStatusSet) {
            String code = sendStatus.getCode();
            if (code == null) {
                throw new RuntimeException("短信发送失败，没有响应 code");
            } else if (!code.equals("OK")) {
                throw new RuntimeException("短信发送失败，错误 Code：" + code + "，错误消息：" + sendStatus.getMessage());
            }
        }


    }
}
